﻿/*
 * KLF Headless Browser
 * Written by Kris Oye <kristianoye@gmail.com>
 * Copyright (C) 2011-2012.  All rights reserved.
 */
namespace KLF.Lib.Browser.DOM.HTML
{
    #region
    
    using System;
    using KLF.Lib.Browser.DOM.Interface;

    #endregion

    public class HTMLScriptElement : HTMLElement, ILoadableContent
    {
        #region Attributes

        /// <summary>
        /// The Javascript source code to execute.
        /// </summary>
        internal string Code { get; set; }

        /// <summary>
        /// For internal use only.  This flag gets set when the script 
        /// has been executed (success or failure).  No script block should
        /// execute more than once.
        /// </summary>
        internal bool HasExecuted { get; set; }

        /// <summary>
        /// Indicates whether this script has been loaded from source.
        /// </summary>
        public bool HasLoaded { get; set; }

        #endregion

        #region Constructors

        internal HTMLScriptElement(Document source)
            : base(source, HTMLConstants.HTML_TAG_SCRIPT)
        {
            HasExecuted = false;
            HasLoaded = false;
        }

        #endregion

        #region DOM Level 2 HTML Compliance

        /// <summary>
        /// 
        /// </summary>
        public string charset
        {
            get
            {
                return getAttribute(HTMLConstants.HTML_ATT_CHARSET);
            }
            set
            {
                setAttribute(HTMLConstants.HTML_ATT_CHARSET, value);
            }
        }

        /// <summary>
        /// Indicates whether the client can defer loading of the script.
        /// </summary>
        public bool defer
        {
            get
            {
                return getAttributeAsType<bool>(HTMLConstants.HTML_ATT_DEFER, false);
            }
            set
            {
                setAttributeAsType<bool>(HTMLConstants.HTML_ATT_DEFER, value);
            }
        }

        /// <summary>
        /// This attribute links this label with another form control by id attribute. 
        /// See the for attribute definition in HTML 4.01.
        /// </summary>
        public string @event
        {
            get
            {
                return getAttribute(HTMLConstants.HTML_ATT_EVENT);
            }
            set
            {
                if (String.IsNullOrEmpty(value))
                    removeAttribute(HTMLConstants.HTML_ATT_EVENT);
                else
                    setAttribute(HTMLConstants.HTML_ATT_EVENT, value);
            }
        }

        /// <summary>
        /// This attribute links this label with another form control by id attribute. 
        /// See the for attribute definition in HTML 4.01.
        /// </summary>
        public string htmlFor
        {
            get
            {
                return getAttribute(HTMLConstants.HTML_ATT_FOR);
            }
            set
            {
                if (String.IsNullOrEmpty(value))
                    removeAttribute(HTMLConstants.HTML_ATT_FOR);
                else
                    setAttribute(HTMLConstants.HTML_ATT_FOR, value);
            }
        }

        /// <summary>
        /// URI [IETF RFC 2396] designating an external script. See the src 
        /// attribute definition in HTML 4.01.
        /// </summary>
        public string src
        {
            get
            {
                return getAttribute(HTMLConstants.HTML_ATT_SRC);
            }
            set
            {
                setAttribute(HTMLConstants.HTML_ATT_SRC, value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string text
        {
            get
            {
                return base.textContent;
            }
            set
            {
                base.textContent = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string type
        {
            get
            {
                return getAttribute(HTMLConstants.HTML_ATT_TYPE);
            }
            set
            {
                setAttribute(HTMLConstants.HTML_ATT_TYPE, value);
            }
        }

        #endregion

        public override string textContent
        {
            get
            {
                return "";
            }
        }
    }
}
